$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var material = "";

    //搜索框 获取焦点
    $("#goods_id").focus();
    $("#goods_id").blur(function () {
        if (focus) {
            $("#goods_id").focus();
        }
    });

    //点击 返回 箭头
    $(".head .goBack").on("click", function () {
        localStorage.removeItem("work_id");
        window.location.href = "ropewire.html";
    });

    //根据扫描内容 得到 原材料
    $(document).on("keydown", "#goods_id", function (event) {
        if (event.keyCode == "13") {
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描干/湿拉原料！");
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            //加载提示符 显示
            $(".content .list-block ul .infinite-scroll-preloader").show();
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/ropewire/getRopewireMaterial',
                data: {
                    id: localStorage.getItem("work_id"),
                    machine: localStorage.getItem("ropewireMachine"),
                    number: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        var data = res.result;
                        //加载提示符 隐藏
                        $(".content .list-block ul .infinite-scroll-preloader").hide();
                        //console.log(data);
                        $alert(res.message);
                        material = data;
                        //填充 页面
                        $(".section span.number").html(data.batch);
                        $(".section span.gggh").html(data.inventorySpecification);
                        $(".section span.lh").html(data.stove);
                        $(".section span.bh").html(data.column);
                        $(".section span.zl").html(data.availableQuantity);
                        $(".section span.pcdh").html(data.workplanNumber);
                    } else {
                        //加载提示符 隐藏
                        $(".content .list-block ul .infinite-scroll-preloader").hide();
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    //加载提示符 隐藏
                    $(".content .list-block ul .infinite-scroll-preloader").hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        }
    });

    //点击 取消 按钮
    $(".foot .quxiao").on("click", function () {
        localStorage.removeItem("work_id");
        window.location.href = "ropewire.html";
    });

    //点击 确定 按钮
    $(".foot .queding").on("click", function () {
        if (!material) {
            $alert("请扫描干/湿拉原料！");
            setTimeout(function () {
                $("#goods_id").val("").focus();
            }, 1400);
        }
        else {
            // if (material.workplanNumber && material.workplanNumber != material.plan.workplanNumber) {
            //     $.modal({
            //         title:  '排产单不一致，是否上料？',
            //         buttons: [
            //             {
            //                 text: '否',
            //                 onClick: function() {
            //
            //                 }
            //             },{
            //                 text: '是',
            //                 onClick: function() {
            //                     loading_show("上料中");
            //                     $.ajax({
            //                         headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            //                         type: "post",
            //                         url: url + '/app/ropewire/feedropewireMachine',
            //                         data: {
            //                             id: localStorage.getItem("work_id"),
            //                             machine: localStorage.getItem("ropewireMachine"),
            //                             number: $(".section span.number").text()
            //                         },
            //                         dataType: 'json',
            //                         json: 'callback',
            //                         success: function (res) {
            //                             if (res.success) {
            //                                 loading_hide();
            //                                 //上料 成功 跳转 页面
            //                                 localStorage.removeItem("work_id");
            //                                 window.location.href = "ropewire.html";
            //                             } else {
            //                                 loading_hide();
            //                                 setTimeout(function () {
            //                                     $("#goods_id").val("").focus();
            //                                 }, 1400);
            //                                 $alert(res.message);
            //                             }
            //                         },
            //                         error: function (error) {
            //                             loading_hide();
            //                             console.log(error);
            //                             setTimeout(function () {
            //                                 $("#goods_id").val("").focus();
            //                             }, 1400);
            //                             $alert(error.responseText);
            //                         }
            //                     });
            //                 }
            //             }
            //         ]
            //     });
            // } else {
                loading_show("上料中");
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/app/ropewire/feedRopewireMachine',
                    data: {
                        planId: material.plan._id,
                        id: localStorage.getItem("work_id"),
                        machine: localStorage.getItem("ropewireMachine"),
                        number: $(".section span.number").text()
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        if (res.success) {
                            loading_hide();
                            //上料 成功 跳转 页面
                            localStorage.removeItem("work_id");
                            window.location.href = "ropewire.html";
                        } else {
                            loading_hide();
                            setTimeout(function () {
                                $("#goods_id").val("").focus();
                            }, 1400);
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        loading_hide();
                        console.log(error);
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(error.responseText);
                    }
                });
            // }
        }
    });

});
